package com.cnzz.site1255174697.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.base.BaseActivity;
import com.cnzz.site1255174697.utils.SystemBarUtils;
import com.cnzz.site1255174697.utils.WebViewUtils;
import com.cqyanyu.yanyu.model.EventEntity;
import com.cqyanyu.yanyu.model.UserInfoEntity;
import com.cqyanyu.yanyu.utils.XToastUtil;

import org.greenrobot.eventbus.EventBus;


/**
 * 没有头部WebView
 */
public class WebActivity extends BaseActivity {
    protected WebView webView;
    protected String titlet;
    private String url1, type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_cart);
        initView();
        SystemBarUtils.setSystemBarColor(this, getWindow().getDecorView());
        Intent intent = getIntent();
        setTitle("加载中...");
        if (intent == null) {
            XToastUtil.showToast(this, "请传入参数");
        } else {
            url1 = intent.getStringExtra("url");
            titlet = intent.getStringExtra("title");
            type = intent.getStringExtra("type");
            if (titlet != null && !titlet.equals("")) {
                setTitle(titlet);
            }
        }
        WebViewUtils.seWebSettingst(webView);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (WebActivity.this.titlet == null) {
                    setTitle(title);
                }
            }

            @Override
            public void onReachedMaxAppCacheSize(long spaceNeeded,
                                                 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
                quotaUpdater.updateQuota(spaceNeeded * 2);
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                if (url.startsWith("https://pass.tmall.com/add")) {
                    //天猫商品
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                } else if (url.startsWith("https://login.m.tmall.com/")) {
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                } else if (url.startsWith("https://login.m.etao.com")) {
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                } else if (url.startsWith("https://h5.m.taobao.com/awp/base/order.htm")) {
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                } else if (url.startsWith("https://login.m.taobao.com/login.htm")) {
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                } else if (url.startsWith("https://buy.m.tmall.com/order/confirmOrderWap.htm")) {
                    EventEntity eventEntity = new EventEntity();
                    eventEntity.type = 222;
                    EventBus.getDefault().post(eventEntity);
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
            }
        });
        webView.loadUrl(url1);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();   //后退
        } else {
            super.onBackPressed();
        }
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.mWebView);
    }
}
